package com.newasia.server.controller.form;

import com.newasia.server.common.exception.ValidateException;
import com.newasia.server.common.loginStrategy.LoginMethod;
import com.newasia.server.common.loginStrategy.LoginStrategy;
import com.newasia.server.common.result.ManagementRetCode;
import com.newasia.server.shiro.MyShiroTaken;
import jodd.util.StringUtil;
import lombok.Data;
import org.apache.shiro.authc.UsernamePasswordToken;

@Data
public class LoginForm implements Form{

    private String username;
    private String password;
    private String method;
    private String code;

    @Override
    public void requestCheck() {
        if(StringUtil.isEmpty(username)||StringUtil.isEmpty(password)){
            throw new ValidateException(
                    ManagementRetCode.REQUEST_CHECK_FAILURE.getCode(),
                    ManagementRetCode.REQUEST_CHECK_FAILURE.getMessage());
        }
    }

    public MyShiroTaken login(){
        LoginMethod loginMethod = LoginStrategy.get(method);
        MyShiroTaken token = loginMethod.getToken(username,password,code);
        return token;
    }
}
